<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>登录</title>
  </head>
  <body>
    <div>
      <label> 用户名 <input type="text" id="textUsername" /> </label>
      <label> 密码 <input type="password" id="textPassword" /> </label>
      <button id="btnLogin">登录</button>
    </div>

    <script src="https://cdn.bootcss.com/jquery/3.3.1/jquery.js"></script>
    <script>
      // 发送 post 请求
      function post(url, data = {}) {
        return $.ajax({
          type: 'post',
          url,
          data: JSON.stringify(data),
          contentType: 'application/json'
        });
      }

      $('#btnLogin').click(() => {
        const username = $('#textUsername').val();
        const password = $('#textPassword').val();
        const url = '/api/user/login';
        const data = {
          username,
          password
        };
        post(url, data).then(res => {
          if (res.code === 0) {
            // 登录成功
            location.href = './admin.html';
          } else {
            // 登录失败
            alert(res.msg);
          }
        });
      });
    </script>
  </body>
</html>
